package com.test.zhangxin.jianpin;

import android.app.Application;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by admin on 2017/6/19.
 */

public class MyApplication extends Application {
    private ServerApi mApi;
    private Retrofit mRetrofit;
    private String HOST = "http://img-wifi.poco.cn/mypoco/mtmpfile/API/";
    public MyApplication() {
        OkHttpClient client = new OkHttpClient();
        if (BuildConfig.DEBUG) {
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                @Override
                public void log(String message) {
                    if (TextUtils.isEmpty(message)) return;
                    String s = message.substring(0, 1);
                    if ("{".equals(s) || "[".equals(s)) {
                        Log.e("zx","收到响应: " + message);
                    }
                }
            });
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
        }
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:ss").create();
        mRetrofit = new Retrofit.Builder().baseUrl(HOST)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(client)
                .build();
    }

    public Retrofit getRetrofit() {
        return mRetrofit;
    }

    public ServerApi getApi() {
        return mApi = mRetrofit.create(ServerApi.class);
    }
}
